package ferp.core.calc.scorer;

import ferp.core.calc.Calculator;
import ferp.core.game.Game;
import ferp.core.game.Settings;
import ferp.core.player.Player;

/**
 * User: olegs
 * Date: 12/3/13 1:40 PM
 */
public class Passing extends Scorer
{
  public static final Passing instance = new Passing();

  public static int getTrickCost(Game g, Settings settings)
  {
    int trickCost = settings.passingTrickCost.ordinal() + 1;

    switch (settings.passingProgression)
    {
      case PP_1111:
        return trickCost;

      case PP_123123:
        return trickCost * (g.consequentPassingSets % 3 + 1);

      case PP_1233:
        return trickCost * (g.consequentPassingSets < 2 ? g.consequentPassingSets + 1 : 3);

      case PP_1244:
        return trickCost * (g.consequentPassingSets < 2 ? g.consequentPassingSets + 1 : 4);

      case PP_1234:
        return trickCost * (g.consequentPassingSets + 1);

      case PP_1248:
        return trickCost * (1 << (g.consequentPassingSets > 8 ? 8 : g.consequentPassingSets));
    }

    return 0;
  }

  @Override
  protected void update(Game g, Settings settings, Calculator c)
  {
    // find minimum number of tricks in order to perform amnesty
    int minTricks = Game.MAXIMUM_TRICKS;

    for (Player p : g.players())
      if (g.getTricks(p.id()) < minTricks)
        minTricks = g.getTricks(p.id());

    long cost = getTrickCost(g, settings);

    for (Player p : g.players())
      if (g.getTricks(p.id()) == 0)
        // in case player took no tricks write into his pool
        c.writeToPool(g, settings, p, cost);
      else
        // otherwise punish for every trick he took (perform amnesty by reducing minTricks)
        c.writeToMountain(g, p, cost * (g.getTricks(p.id()) - minTricks));
  }

  @Override
  protected void statistics(Game g)
  {
    g.statistics.sets.passing++;

    for (Player p : g.players())
      g.statistics.passingTricks[p.id()] += g.getTricks(p.id());
  }
}
